#include <list>
#include <deque>
#include <cstring>


//user's include files
#include "map_build.h"
#include "snake_define.h"
#include "food_define.h"

extern std::list<Snake> snake_list;


void Map::draw_snake()
{
    for(auto iter=snake_list.begin(); iter!=snake_list.end(); iter++)//遍历每一条蛇，然后遍历一条蛇的每一段
    {
        std::deque<SnakeNode> temp_snake = iter->get_snake_total();
    
        auto siter = temp_snake.begin();//蛇头
        draw_snake_part(HEAD_TYPE, siter->x, siter->y, siter->dir);
        
        for(;siter!=(temp_snake.end()-2); siter++)//蛇身:此时有一个疑问：中间的判断，假如蛇身不够那么长，那该怎么办（初始化蛇的时候，蛇最短为3）
        {
            draw_snake_part(BODY_TYPE, siter->x, siter->y, siter->dir);
        }
        draw_snake_part(SECOND_TAIL_TYPE, siter->x, siter->y, siter->dir);//画蛇次尾巴
        siter++;
        draw_snake_part(TAIL_TYPE, siter->x, siter->y, siter->dir);//画蛇尾巴
    }
}


/*
*@prelog:蛇有四部分构成——蛇头、蛇身、蛇尾、蛇倒二尾巴，它们的图片是不同的，不能一律按蛇身算
*        对于每条蛇来说，它的每一个点都应该显示出来
*@func:
*@param:类型type:三个参数 TAIL/
*/
void Map::draw_snake_part(PicType type,int x, int y, SnakeDir dir)
{
    PicCode head_code[] = {snake_head_up, snake_head_down, snake_head_left, snake_head_right};
    PicCode tail_code[] = {snake_tail_up, snake_tail_down, snake_tail_left, snake_tail_right};
    PicCode second_tail_code[] = {snake_tail_second_up, snake_tail_second_down, snake_tail_second_left, snake_tail_second_right};

    PicCode *codep = NULL;
    switch(type)
    {
        case BODY_TYPE:
            //m_map[x][y] = snake_body;
            m_map[y][x] = snake_body;
            return;//如果是BODY_TYPE,到此，函数结束运行
            break;
        case HEAD_TYPE:
            codep = head_code;
            break;
        case TAIL_TYPE:
            codep = tail_code;
            break;
        case SECOND_TAIL_TYPE:
            codep = second_tail_code;
            break;
    }
    switch(dir)
    {
        case UP:
            //m_map[x][y] = codep[0];
            m_map[y][x] = codep[0];
            break;
        case DOWN:
            //m_map[x][y] = codep[1];
            m_map[y][x] = codep[1];
            break;
        case LEFT:
            //m_map[x][y] = codep[2];
            m_map[y][x] = codep[2];
            break;
        case RIGHT:
            //m_map[x][y] = codep[3];
            m_map[y][x] = codep[3];
            break;
    }
}

extern std::list<FoodNode> food_list;
void Map::draw_food()
{
    for(auto iter=food_list.begin(); iter!=food_list.end(); iter++)
    {
        //m_map[iter->m_x][iter->m_y] = food;
        m_map[iter->m_y][iter->m_x] = food;
    }
    return;
}

